package com.filyar.htyd.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.filyar.htyd.common.Result;
import com.filyar.htyd.entity.AddressCompareResult;
import com.filyar.htyd.service.IAddressCompareResultService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author filyar
 * @e-mail root@filyar.com
 * @since 2024-11-01
 */
@RestController
@RequestMapping("/addressCompareResult")
public class AddressCompareResultController {

    @Resource
    private IAddressCompareResultService addressCompareResultService;

    private final String now = DateUtil.now();

    @PostMapping
    public Result save(@RequestBody AddressCompareResult addressCompareResult) {
        addressCompareResultService.saveOrUpdate(addressCompareResult);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        addressCompareResultService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        addressCompareResultService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(addressCompareResultService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String address,
                           @RequestParam(defaultValue = "") double likePercent,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        LambdaQueryWrapper<AddressCompareResult> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(AddressCompareResult::getLikePercent);
        if (!address.isEmpty()) {
            lambdaQueryWrapper.like(AddressCompareResult::getAddress, address);
        }
        lambdaQueryWrapper.ge(AddressCompareResult::getLikePercent, likePercent);
        return Result.success(addressCompareResultService.page(new Page<>(pageNum, pageSize), lambdaQueryWrapper));
    }


}

